package com.hanhanxiaochong.zhongxiangchuanmei.view.my.act;

import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.LoginInfoBean;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.BindPhonePresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.util.PhonevalidationUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ResourcesUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.util.RxTimerUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.util.SharedPreferencesUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ToastUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.IBindPhoneView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/4/17.
 * Desc   : 绑定手机号act
 */

public class BindPhoneActivity extends BaseActivity<BindPhonePresenter> implements IBindPhoneView {
    @BindView(R.id.img_left)
    ImageView imgLeft;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_phone_number)
    TextView txtPhoneNumber;
    @BindView(R.id.et_phone_number)
    EditText etPhoneNumber;
    @BindView(R.id.txt_get_yanzhengma)
    TextView txtGetYanzhengma;
    @BindView(R.id.et_yanzheng)
    EditText etYanzheng;
    private String token;
    private SharedPreferencesUtil sharedPreferencesUtil;
    private LoginInfoBean loginInfo;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_bind_phone);
        sharedPreferencesUtil = SharedPreferencesUtil.getsInstances(this);
        loginInfo = sharedPreferencesUtil.getLoginInfo();
        token = loginInfo.getToken();
    }

    @Override
    protected void initPresenter() {
        mPresenter = new BindPhonePresenter(this);
    }

    @Override
    protected void init() {
        setTopView();
        txtPhoneNumber.setText(loginInfo.getMobile());
    }

    private void setTopView() {
        txtTitle.setText("绑定手机号");
        imgLeft.setImageResource(R.mipmap.icon_left);
    }

    @OnClick({R.id.ll_leftview, R.id.txt_get_yanzhengma, R.id.btn_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_leftview:
                finish();
                break;
            case R.id.txt_get_yanzhengma:
                String phoneNum = etPhoneNumber.getText().toString().trim();
                if (PhonevalidationUtil.isMobileNO(phoneNum)) {
                    mPresenter.sendMsg(phoneNum, "1");
                    RxTimerUtil.interval(1, 60, new RxTimerUtil.IRxNext() {
                        @Override
                        public void doNext(long number) {
                            txtGetYanzhengma.setClickable(false);
                            txtGetYanzhengma.setText(String.format(ResourcesUtils.getString(R.string.repeat_verify_code_hint), 60 - number));
                        }

                        @Override
                        public void doComplete() {
                            txtGetYanzhengma.setClickable(true);
                            txtGetYanzhengma.setText("验证");
                        }
                    });
                } else {
                    ToastUtils.showToast("手机号格式错误");
                }
                break;
            case R.id.btn_confirm:
                mPresenter.bindPhoneNumber(token, etPhoneNumber.getText().toString().trim(), etYanzheng.getText().toString().trim());
                break;
        }
    }

    @Override
    public void bindSuccess() {
        ToastUtils.showToast("绑定成功");
        loginInfo.setMobile(etYanzheng.getText().toString().trim());
        sharedPreferencesUtil.setLoginInfo(loginInfo);
        finish();
    }
}
